package com.zcsy.manage.owner.controller.order;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.OwnerConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.manage.model.base.CommonToken;
import com.zcsy.manage.model.mall.OrderDetailInfo;
import com.zcsy.manage.model.mall.OrderInfo;
import com.zcsy.manage.owner.service.common.IOCommonTokenService;
import com.zcsy.manage.service.mall.IGoodsInfoService;
import com.zcsy.manage.service.mall.IOrderDetailInfoService;
import com.zcsy.manage.service.mall.IOrderInfoService;

/**
 * @Title: OrderInfoController.java
 * @Package： com.zcsy.manage.owner.controller.order 
 * @Description: 订单控制层
 *
 * @author caiyinli 13277030520@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年7月10日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "orderInfo")
public class OrderInfoController {
	
	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private IOrderDetailInfoService orderDetailInfoService;

	@Autowired
	private IOCommonTokenService commomTokenService;
	
	@Autowired
	private IGoodsInfoService goodsInfoService;

	/**
	 * @Description 获取我的订单列表接口<br>
	 * @param pNo
	 * @param state
	 * @param token
	 * @return
	 */
	@RequestMapping(value = "getMyOrder")
	@ResponseBody
	public RequestJson getMyOrder(Integer pNo,Integer state,String token) {
		
		RequestJson result = new RequestJson();
		try {
			result = commomTokenService.OrderInteractState(token, result);
			if (result.isSuccess()) {
				CommonToken tokenInfo = commomTokenService.selectByToken(token);
				
				if(pNo == null){
					pNo = Constants.PAGE_NO;
				}
				
				//此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
				PageHelper.startPage(pNo, Constants.PAGE_SIZE);
				
				if(state == null){
					state = Constants.ORDER_ALL;
				}
				
				// 根据token查询登录名
				String loginname = tokenInfo.getAccount();
				//用于存放订单id
				List<OrderInfo> orders = orderInfoService.selectOrderInfoAll(state,loginname);
				
				if (CollectionUtils.isNotEmpty(orders)) {
					PageInfo<OrderInfo> info = new PageInfo<OrderInfo>(orders);
					//待付款的母订单对应商品需要再查一次
					for(OrderInfo item : info.getList()){
						if(item.getOrderLevel() == Constants.ORDER_STATE_FIRST){
							if(org.springframework.util.StringUtils.isEmpty(item.getShopName())){
								item.setShopName(Constants.SHOPS);
							}
							item.setOrderDetailList(orderDetailInfoService.getOrderDetailWhenFirst(item.getId()));
						}
						
						if(item.getType().intValue() != Constants.GOODS_TYPE_NORMAL){
							item.setShopName(Constants.DLIFE);
						}
					}
					
					
					result.setResultCode(OwnerConstants.RESULT_CODE_SUCCESS);
					result.setMsg("数据获取成功！");
					result.setObj(info);
				}else {
					result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
					result.setMsg("无数据！");
					result.setObj(orders);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
			result.setMsg("网络异常！");
		}
		return result;
	}
	
	/**
	 * @Description 获取订单详情接口<br>
	 * 
	 * @param orderId 订单id
	 * @param token token表示
	 * @return 返回订单详情
	 * @author caiyinli 13277030520@163.com
	 * @date 2016年7月12日
	 * @see 
	 */
	@RequestMapping(value = "getOrderDetail")
	@ResponseBody
	public RequestJson getOrderDetail(String orderId,String token) {
		
		RequestJson result = new RequestJson();
		try {
			result = commomTokenService.OrderInteractState(token, result);
			if (result.isSuccess()) {
				OrderInfo orderInfo = orderInfoService.selectOrderByPrimaryKey(orderId);
				if(orderInfo != null){
					if(orderInfo.getType().intValue() != Constants.GOODS_TYPE_NORMAL){
						orderInfo.setShopName(Constants.DLIFE);
					}
					
					List<OrderDetailInfo> orderDetailList;
					//母订单需关联查询
					if(orderInfo.getOrderLevel() == Constants.ORDER_STATE_FIRST){
						orderDetailList = orderDetailInfoService.getOrderDetailInfosByParentOrder(orderId);
						if(org.springframework.util.StringUtils.isEmpty(orderInfo.getShopName())){
							orderInfo.setShopName(Constants.SHOPS);
						}
					}else{
						orderDetailList = orderDetailInfoService.getOrderDetailInfos(orderId);
					}
					
					if (orderDetailList != null) {
						orderInfo.setOrderDetailList(orderDetailList);
						result.setResultCode(OwnerConstants.RESULT_CODE_SUCCESS);
						result.setMsg("数据获取成功！");
						result.setObj(orderInfo);		
						
					} else {
						result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
						result.setMsg("无订单详情数据！");
						result.setObj(orderInfo);

					}
				}else{
					result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
					result.setMsg("无数据！");
					result.setObj(orderInfo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
			result.setMsg("网络异常！");
		}
		return result;
	}
	
	/**
	 * @Description 取消订单，退款申请，确认收货接口<br>
	 * 
	 * @param orderId 订单id
	 * @param state 订单状态
	 * @param reason 退款理由
	 * @param token token标示
	 * @return json对象
	 * @author caiyinli 13277030520@163.com
	 * @date 2016年7月12日
	 * @see 
	 */
	@RequestMapping(value = "updateOrderState")
	@ResponseBody
	public RequestJson updateOrderState(String orderId,Integer state,String token,String reason) {
		
		RequestJson result = new RequestJson();
		try {
			result = commomTokenService.OrderInteractState(token, result);
			if (result.isSuccess()) {
				Date now = new Date();
				int flag = Constants.SQL_RESULT_ERROR;
				OrderInfo orderInfo = orderInfoService.selectOrderByPrimaryKey(orderId);
				orderInfo.setState(state);
				
				switch (state) {
				case OwnerConstants.FINISH_ORDER:
					orderInfo.setConfirmTime(now);
					break;
				case OwnerConstants.DROP_ORDER:
					orderInfo.setCancelTime(now);
					orderInfo.setCancelType(Constants.ORDER_CANCEL_TYPE_OWNER);
					orderInfo.setCancelAccount(result.getMsg());
					break;
				case OwnerConstants.APPLY_REFUND_ORDER:
					orderInfo.setReason(reason);
					orderInfo.setRefundRequestTime(now);
					break;
				case OwnerConstants.APPLY_RETURN_GOODS_ORDER:
					orderInfo.setReason(reason);
					orderInfo.setReturnRequestTime(now);
					break;
				}
				
				flag = orderInfoService.update(orderInfo);
				
				List<OrderDetailInfo> orderDetailInfos;
				if(orderInfo.getOrderLevel() == Constants.ORDER_STATE_FIRST){
					orderDetailInfos = orderDetailInfoService.getOrderDetailInfosByParentOrder(orderId);
					
					//同时更改子订单状态
					OrderInfo order = new OrderInfo();
					order.setParentId(orderId);
					order.setState(state);
					orderInfoService.updateByParentOrder(order);
				}else{
					orderDetailInfos = orderDetailInfoService.getOrderDetailInfos(orderId);
				}
				
				//支付订单、取消订单修改商品库存和销量
				if(state == OwnerConstants.DROP_ORDER){
					goodsInfoService.updateGoodsInfo(orderDetailInfos, orderInfo.getState());
				}
				if(flag == Constants.SQL_RESULT_SUCCESS){
					switch (state) {
					case OwnerConstants.FINISH_ORDER:
						result.setMsg("操作成功！");
						break;
					case OwnerConstants.DROP_ORDER:
						result.setMsg("取消成功！");
						break;
					case OwnerConstants.APPLY_REFUND_ORDER:
						result.setMsg("退款申请成功！");
						break;
					case OwnerConstants.APPLY_RETURN_GOODS_ORDER:
						result.setMsg("退货申请成功！");
						break;

					}
					result.setResultCode(OwnerConstants.RESULT_CODE_SUCCESS);
					result.setSuccess(true);
				}else {
					result.setMsg("操作失败");
					result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
					result.setSuccess(false);
				}
				
			}
		} catch (Exception e) {
			result.setResultCode(OwnerConstants.RESULT_CODE_FALSE);
			result.setMsg("网络异常！");
		}
		return result;
	}
}
